package com.qf.controller;

import com.qf.dto.R;
import com.qf.dto.UserDto;
import com.qf.pojo.SysUserInfo;
import com.qf.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * created by 赵文韬 on 2022/3/17 10:28
 */
/*完成添加和注册用户的Controller
* 需要返回一个json对象，参数也需要传递一个userdtojson对象*/
@RestController
@RequestMapping("/users")
public class UserController {
    private SysUserService sysUserService;
    @Autowired
    public void setSysUserService(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }
    //添加用户
    @PostMapping("/user")
    public R registry(@RequestBody UserDto userDto){
        SysUserInfo userInfo = new SysUserInfo();
        BeanUtils.copyProperties(userDto,userInfo );
        sysUserService.addUser(userInfo);
        return R.setOk();

    }
    //registry，参数时userDto对象
    //把userDto转到userInfo中，然后再调用userInfo的addUserInfo的方法
    //最后返回R.setOk();

    //先写注册
    @PostMapping("/login")
    public R login(@RequestBody UserDto userDto, HttpSession session){
        SysUserInfo userInfo = sysUserService.login(userDto);
        session.setAttribute("user",userInfo );
        return R.setOk();
    }
    //参数需要一个dto对象，和一个session
    //调用service的login方法，看是有没有返回userinfo对象
    //返回的画把对象放到session域

}
